package cn.paul.wechatshop.utils;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

/**
 * Created by 徐凌峰 on 2017/9/26 0026.
 * 用于ftp上传的工具类
 */
public class FtpUtil {
    private static final Logger logger = LoggerFactory.getLogger(FtpUtil.class);

    private static String ftpIp = PropertiesUtil.getProperty("ftp.server.ip");
    private static String ftpUser = PropertiesUtil.getProperty("ftp.user");
    private static String ftpPassword = PropertiesUtil.getProperty("ftp.pass");
    private String ip;
    private String username;
    private String password;
    private FTPClient ftpClient = null;

    public FtpUtil(String ip,String username, String password) {
        this.ip = ip;
        this.username = username;
        this.password = password;
    }

    public static boolean uploadFile(List<File> fileList) throws IOException {
        FtpUtil ftpUtil = new FtpUtil(ftpIp,ftpUser, ftpPassword);
        logger.info("开始连接ftp服务器");
        boolean result = ftpUtil.uploadFile("/img", fileList);
        logger.info("上传结束,结果为{}",result);
        return result;
    }

    private boolean uploadFile(String remotePath, List<File> fileList) throws IOException {
        FileInputStream fileInputStream = null;
        //连接ftp服务器
        if (connectServer(ip, username, password)) {
            try {
                ftpClient.changeWorkingDirectory(remotePath);
                ftpClient.setBufferSize(1024);
                ftpClient.setControlEncoding("utf-8");
                ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
                ftpClient.enterLocalPassiveMode();
                for (File fileItem : fileList) {
                    fileInputStream = new FileInputStream(fileItem);
                    ftpClient.storeFile(fileItem.getName(), fileInputStream);
                }
                return true;
            } catch (IOException e) {
                logger.error("上传文件异常", e);
                e.printStackTrace();
            } finally {
                assert fileInputStream != null;
                fileInputStream.close();
                ftpClient.disconnect();

            }

        }
        return false;
    }

    private boolean connectServer(String ip, String username, String password) {
        ftpClient = new FTPClient();
        try {
            ftpClient.connect(ip,21);
            return ftpClient.login(username, password);
        } catch (IOException e) {
            logger.error("连接ftp服务器失败", e);
            return false;
        }
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }



    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }


}
